# -*- coding: utf-8 -*-
import json
import sys
import time
import pymysql
from handle_db import db_data
from function import *
from urllib import parse
import warnings
warnings.filterwarnings("ignore")

# 这个地方必须这么写 函数名：response
def response(flow):
    # 通过抓包软包软件获取请求的话题列表接口(话题列表接口)
    if 'aweme/v1/challenge/search/?cursor=0&keyword=' in flow.request.url:

        # 数据的解析
        dict_json = json.loads(flow.response.text)
        # 获取关键词
        keyword = get_keyword(flow.request.url)
        challenge_list = dict_json['challenge_list']
        for challengeinfo in challenge_list:
            # 组建话题视频作者信息数据
            user_info = challengeinfo.get('challenge_info', '')
            user_info_data = {}
            # 当前话题信息
            user_info_data['keyword'] = keyword
            user_info_data['cha_name'] = user_info.get('cha_name', '')
            user_info_data['view_count'] = user_info.get('view_count', '')
            user_info_data['user_count'] = user_info.get('user_count', '')
            user_info_data['cid'] = user_info.get('cid', '')
            # 作者信息
            author_info = user_info['author']
            user_info_data['uid'] = author_info.get('uid', '')
            user_info_data['unique_id'] = author_info.get('unique_id', '')
            user_info_data['language'] = author_info.get('language', '')
            user_info_data['signature'] = author_info.get('signature', '')
            user_info_data['short_id'] = author_info.get('short_id', '')
            user_info_data['avatar_uri'] = author_info.get('avatar_uri', '')
            user_info_data['nickname'] = author_info.get('nickname', '')
            user_info_data['status'] = author_info.get('status', '')
            user_info_data['avatar_larger'] = ','.join(author_info['avatar_larger'].get('url_list', ''))
            user_info_data['region'] = author_info.get('region', '')
            user_info_data['birthday'] = author_info.get('birthday', '')
            user_info_data['share_qrcode_uri'] = author_info.get('share_qrcode_uri', '')
            user_info_data['unique_id_modify_time'] = author_info.get('unique_id_modify_time', '')
            user_info_data['user_rate'] = author_info.get('user_rate', '')
            user_info_data['gender'] = author_info.get('gender', '')
            user_info_data['bind_phone'] = author_info.get('bind_phone', '')
            user_info_data['live_agreement_time'] = author_info.get('live_agreement_time', '')
            user_info_data['live_agreement'] = author_info.get('live_agreement', '')
            user_info_data['create_time'] = author_info.get('create_time', '')
            # 话题视频作者信息插入话题作者信息表
            db_data.handle_data_add('douyin_video_topic', user_info_data)

    # 通过抓包软包软件获取请求的接口(作者话题视频列表接口)
    if 'aweme/v1/challenge/aweme/?ch_id=' in flow.request.url:
        # 数据的解析
        dict_json = json.loads(flow.response.text)
        # 只能存储作者的，其他用户的不存储
        # 获取任意表里面的单个字段的集合(get_field:需要获取信息的字段, table:表明)
        # 组建话题视频信息数据
        for video in dict_json.get('aweme_list', ''):
            # 获取用户uid,也就是分享id,然后取出使用(field:需要取出的字段,table:表名)
            author_cid = db_data.get_table_info('cid', 'douyin_video_topic')
            cid = str(video['cha_list'][0]['cid'])
            hashtag_name = str(video['cha_list'][0]['cha_name'])

            # print(author_cha_name[0],hashtag_name, '00000000')
            # 二维元组变一维列表
            list_author_cid = handle_tuple(author_cid)
            # 如果uid存在于作者列表中中，再提取视频相应的数据
            if cid in list_author_cid:
                print(hashtag_name, '1111')
                # 组建视频信息数据
                video_data = {}
                video_data['aweme_id'] = video.get('aweme_id', '')
                video_data['hashtag_name'] = hashtag_name
                video_data['top_cid'] = cid
                video_data['author_user_id'] = video.get('author_user_id', '')
                video_data['desc'] = video.get('desc', '')
                video_data['create_time'] = video.get('create_time', '')
                video_data['digg_count'] = video['statistics'].get('digg_count', '')
                video_data['share_count'] = video['statistics'].get('share_count', '')
                video_data['forward_count'] = video['statistics'].get('forward_count', '')
                video_data['comment_count'] = video['statistics'].get('comment_count', '')
                video_data['share_url'] = video.get('share_url', '')
                video_data['rate'] = video.get('rate', '')
                video_data['region'] = video.get('region', '')
                if video['video'] is None:
                    pass
                else:
                    video_data['play_addr'] = ','.join(video['video']['play_addr_lowbr']['url_list'][0:2])
                    video_data['origin_cover'] = ','.join(video['video']['origin_cover']['url_list'])
                    video_data['ratio'] = video['video'].get('ratio', '')
                video_data['group_id'] = video.get('group_id', '')
                video_data['reviewed'] = video['status'].get('reviewed', '')
                video_data['allow_share'] = video['status'].get('allow_share', '')
                video_data['private_status'] = video['status'].get('private_status', '')
                video_data['download_status'] = video['status'].get('download_status', '')
                video_data['allow_comment'] = video['status'].get('allow_comment', '')
                # 话题视频信息数据插入到视频信息数据表
                db_data.handle_data_add('douyin_video', video_data)

                # 组建话题视频音乐信息数据
                music = video.get('music', '')
                music_data = {}
                # 如果是字典了再往下走
                if type(music).__name__ == 'dict':
                    music_data['mid'] = music.get('mid', '')
                    music_data['id'] = music.get('id', '')
                    music_data['aweme_id'] = video.get('aweme_id', '')
                    music_data['title'] = music.get('title', '')
                    music_data['status'] = music.get('status', '')
                    music_data['owner_handle'] = music.get('owner_handle', '')
                    music_data['owner_nickname'] = music.get('owner_nickname', '')
                    music_data['owner_id'] = music.get('owner_id', '')
                    music_data['duration'] = music.get('duration', '')
                    music_data['play_url'] = music.get('play_url', '').get('uri', '')
                    music_data['source_platform'] = music.get('source_platform', '')
                    music_data['hashtag_name'] = hashtag_name
                    # 话题视频音乐信息插入视频音乐信息表
                    db_data.handle_data_add('douyin_video_music', music_data)